package com.bp.hearthealth.pulse.lnapps.bp_db.bp_project

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import androidx.room.Room
import com.bp.hearthealth.pulse.lnapps.bp_db.bp_bloodpressure.BpBloodPressureDao
import com.bp.hearthealth.pulse.lnapps.bp_db.bp_heartrate.BpHeartRateDao
import com.bp.hearthealth.pulse.lnapps.bp_db.bp_user.BpUserDao

class BpDataBaseManager {
    private var mContext: Context? = null
    var appDatabase: BpAppDatabase? = null

    companion object {

        @SuppressLint("StaticFieldLeak")
        @Volatile
        private var mInstance: BpDataBaseManager? = null

        fun getInstance(): BpDataBaseManager {
            if (mInstance == null) {
                synchronized(BpDataBaseManager::class.java) {
                    if (mInstance == null) {
                        mInstance = BpDataBaseManager()
                    }
                }
            }
            return mInstance!!
        }
    }

    fun init(app: Application) {
        mInstance?.run {
            if (mContext == null) {
                mContext = app.applicationContext
            }
            appDatabase = Room.databaseBuilder(
                mContext!!, BpAppDatabase::class.java, BpDbConstants.databaseName
            ).allowMainThreadQueries().fallbackToDestructiveMigration().addMigrations(
            ).build()
        }
    }


    fun getUser(): BpUserDao = appDatabase!!.userDao()
    fun getHeartRate(): BpHeartRateDao = appDatabase!!.heartRateDao()
    fun getBloodPressure(): BpBloodPressureDao = appDatabase!!.bloodPressure()

}

